//
//  ViewController.m
//  WebViewPreload
//
//  Created by tianlong on 2018/2/12.
//  Copyright © 2018年 tianlong. All rights reserved.
//

#import "ViewController.h"
#import "TLEngine.h"
#import <WebKit/WebKit.h>

/**
 * HTTP Header:Content-Type
 */
#define HTTPHeaderKeyContentType @"Content-Type"

@interface ViewController ()<TLEngineDelegate,UIWebViewDelegate>
@property (strong, nonatomic) TLEngine *engine;
@property (weak, nonatomic) UIWebView *webView;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (IBAction)loadJD:(id)sender {
    NSString *url = @"https://m.jd.com";
    self.engine = [[TLEngine alloc] initWithUrl:url delegate:self delegateQueue:[NSOperationQueue currentQueue]];
    [self.engine start];
}

- (IBAction)loadJDHtmlString:(id)sender {
    if (nil == _webView) {
        UIWebView *web = [[UIWebView alloc] initWithFrame:CGRectMake(10, 250, self.view.bounds.size.width-20, self.view.bounds.size.height-250)];
        web.backgroundColor = [UIColor greenColor];
        web.delegate = self;
        self.webView = web;
        [self.view addSubview:web];
    }
    
    if (self.engine.htmlString) {
       [self.webView loadHTMLString:self.engine.htmlString baseURL:[NSURL URLWithString:@"http:"]];
    }
}


#pragma mark - TLEngineDelegate
- (void)engine:(TLEngine *)engine didRecieveResponse:(NSHTTPURLResponse *)response{
    
}

- (void)engine:(TLEngine *)engine didReceiveData:(NSData *)data{
    
}

- (void)engineDidCompleteWithoutError:(TLEngine *)engine{
    
}

- (void)engine:(TLEngine *)engine didCompleteWithError:(NSError *)error{
    
}

#pragma mark - UIWebViewDelegate
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
    return YES;
}

- (void)webViewDidStartLoad:(UIWebView *)webView{
    NSLog(@"%s",__FUNCTION__);
}

- (void)webViewDidFinishLoad:(UIWebView *)webView{
    NSLog(@"%s",__FUNCTION__);
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
    NSLog(@"%s",__FUNCTION__);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


@end
